<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Node22-Crud</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="any" href="favicon.ico">
    <link rel="stylesheet" href="css/layui.css">
    <style>
        html, body {
            margin: 20px;
        }
    </style>
</head>
<body>
<div>
    <h2>user表中的数据</h2>
    <hr class="layui-border-green"/>
    <button type="button" class="layui-btn" onclick="init()">刷新数据</button>
    <div class="table-container">
        <table class="layui-table" lay-even>
            <colgroup>
                <col>
                <col>
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>密码</th>
            </tr>
            </thead>
            <tbody id="userTableBody">
            <!-- 数据将通过JavaScript插入 -->
            </tbody>
        </table>
        <div id="demo-laypage-normal-2"></div>
    </div>

    <h2>更新user表</h2>
    <hr class="layui-border-green"/>
    <form class="layui-form" method="post" action="/update">
        <div class="layui-form-item">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-block">
                <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <h2>删除user表中的行</h2>
    <hr class="layui-border-green"/>
    <form class="layui-form" method="post" action="/delete">
        <div class="layui-form-item">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-block">
                <input type="text" name="id" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="js/layui.js"></script>
<script>
    let currentPage = null;

    async function getPage(page) {
        const response = await fetch(`/listPage?page=${page}`)
        currentPage = await response.json()
    }

    function updatePage(userPage) {
        // 渲染分页
        layui.use(['laypage'], function () {
            let laypage = layui.laypage;
            laypage.render({
                elem: 'demo-laypage-normal-2',
                count: userPage.total,
                limit: userPage.pageSize,
                curr: userPage.page,
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: async function (obj) {
                    await getPage(obj.curr)
                    listUsers(currentPage)
                }
            });
        });
    }

    function listUsers(userPage) {
        // 清空现有的表格数据
        const tbody = document.getElementById('userTableBody')
        tbody.innerHTML = ''
        // 插入新的表格数据
        userPage.data.forEach(user => {
            const row = tbody.insertRow()
            const idCell = row.insertCell(0)
            idCell.textContent = user.id
            const usernameCell = row.insertCell(1)
            usernameCell.textContent = user.username
            const passwordCell = row.insertCell(2)
            passwordCell.textContent = user.password
        });
    }
    async function init() {
        await getPage(1)
        updatePage(currentPage)
    }

    window.onload = init

</script>
</body>
</html>